package com.ug.common.utils;

import java.util.concurrent.*;

/**
 * @author: LY
 * @date: 2024/12/23 16:27
 * @description: 公共线程池
 */
public class ThreadPoolUtil {

    private static final ThreadPoolExecutor threadPoolExecutor;

    static {
        int corePoolSize = Runtime.getRuntime().availableProcessors(); // 根据 CPU 核心数调整
        int maxPoolSize = corePoolSize * 2;
        long keepAliveTime = 30L; // 缩短线程空闲时间
        threadPoolExecutor = new ThreadPoolExecutor(
                corePoolSize,
                maxPoolSize,
                keepAliveTime, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(512)
        );
    }

    public static ThreadPoolExecutor getInstance(){
        return threadPoolExecutor;
    }

    public static void submit(Runnable task){
        threadPoolExecutor.submit(task);
    }

    public static void execute(Runnable task){
        threadPoolExecutor.execute(task);
    }

    public static void shutdown(){
        threadPoolExecutor.shutdown();
    }
}
